import { formatGMTDate, toPercent, arrayToObject, compareObject } from '../../../../assets/js/util.js'
export default {
    data() {
        return {
            pickerOptions0: {
              disabledDate(time) {
                return time.getTime() > Date.now() - 8.64e7;
              }
            },
            title: '商城统计',
            xlsxTitle:'',
            SRC: '',
            DP: {
                p: '',
                f: ''
            },
            loading: false,
            searchable: false,
            pickerOptions: {
                shortcuts: [{
                    text: '最近一周',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                        picker.$emit('pick', [start, end]);
                    }
                }, {
                    text: '最近15天',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 15);
                        picker.$emit('pick', [start, end]);
                    }
                }, {
                    text: '最近一个月',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                        picker.$emit('pick', [start, end]);
                    }
                }]
            },
            dialogFormVisible: false,
            date: '',
            //默认的表单参数
            form: {
                dstart: '', //开始时间
                dend: '', //结束时间
                server: '0', //区服id
                param: '', //物品
                reason: '-99999', //商城 默认-99999 全部商城
                rolelevel: -1, //角色等级，默认 无筛选等级
                serverItmes: [],
                reasonItmes: [],
                rolelevelMap: []
            },
            inputWidth: '220px',
            formLabelWidth: '100px',
            /*当前页显示数量*/
            pageSize: 10,
            /*每页显示的数量*/
            pageSizes: [10, 20, 50, 100],
            /*dataTotal表数量*/
            dataTotal: 0,
            /*分页器*/
            currentPage: 1,
            shopAnalysisTableHead: [
                { prop: "day", label: "时间" },
                { prop: "currencytypeStr", label: "货币类型" },
                { prop: "mallName", label: "商城名称" },
                { prop: "levelName", label: "等级" },
                { prop: "itemName", label: "物品" },
                { prop: "sumchangenum", label: "货币消耗量" },
                { prop: "times", label: "购买次数" },
                { prop: "rolenum", label: "购买角色数" },
                { prop: "allSumchangenum", label: "货币消耗占比" },
                { prop: "allRolenum", label: "角色占比" }
            ],
            shopAnalysisTableData: [],
            shopAnalysisTableDataAll: []
        }
    },
    computed: {
        // xlsx() {
        //     return this.title + '.xlsx'
        // }
    },
    methods: {
        //默认成功处理函数
        successFun(data) {
            this.$message({
                message: data,
                type: 'success'
            });
        },
        //默认错误处理函数
        errorFun(data) {
            this.$message.error(data);
        },
        loads() {
            this.loading = !this.loading; //加载中
            this.searchable = !this.searchable; //查询按钮不可用
        },
        //时间变化
        dateChange(val) {
            var t = val.split(' - ')
            this.form.dstart = t[0];
            this.form.dend = t[1];
        },
        //重置表单
        reset(form) {
            this.$refs[form].resetFields();
        },
        //提交表单
        search(form) {
            var that = this;
            that.loads(); //加载 查询按钮不可用
            var formParam = `dstart=${that.form.dstart}&dend=${that.form.dend}&server=${that.form.server}&param=${that.form.param}&reason=${that.form.reason}&rolelevel=${that.form.rolelevel}`
            this.$http.jsonp(`${this.SRC}data/get.do`, {
                params: {
                    p: this.DP.p,
                    f: this.DP.f,
                    a: "search",
                    ps: formParam
                }
            }).then(function(res) {
                that.loads();
                var data = JSON.parse(JSON.parse(res.body))
                if (parseInt(data.res) === 0) {
                    that.shopAnalysisTableDataAll = data.dataList1;
                    that.formatData();
                    that.dataTotal = that.shopAnalysisTableDataAll.length;
                    that.shopAnalysisTableData = that.shopAnalysisTableDataAll.slice((that.currentPage - 1) * that.pageSize, that.currentPage * that.pageSize);
                } else {
                    that.errorFun(data.msg)
                }
            });
        },
        // 表格下载
        excel(a,b,sheet) {
            this.xlsxTitle = sheet+"-"+this.form.dstart+"到"+this.form.dend;
            this.downloadExl(this.shopAnalysisTableHead, this.shopAnalysisTableDataAll,this.xlsxTitle,this.title);
        },
        /*分页器*/
        handleSizeChange(val) {
            this.pageSize = val;
            this.tableControl();
        },
        handleCurrentChange(val) {
            this.currentPage = val;
            this.tableControl();
        },
        tableControl() {
            this.shopAnalysisTableData = this.shopAnalysisTableDataAll.slice((this.currentPage - 1) * this.pageSize, this.currentPage * this.pageSize);
        },
        /*获取初始数据*/
        getData: function() {
            this.$http.jsonp(`${this.SRC}data/get.do`, {
                params: {
                    p: this.DP.p,
                    f: this.DP.f,
                    a: "initParam",
                    ps: ""
                }
            }).then(function(res) {
                var data = JSON.parse(JSON.parse(res.body));
                if (parseInt(data.res) == 0) {
                    this._dataPush(data.serverItmes, this.form.serverItmes);
                    this._dataPush(data.reasonItmes, this.form.reasonItmes);
                    this._dataPush(data.rolelevelMap, this.form.rolelevelMap);
                } else {
                    this.errorFun(data.msg);
                }
            })
        },
        _dataPush(firstData, lastData) {
            for (var key in firstData) {
                lastData.push({ label: key, value: firstData[key] })
            }
        },
        //占百分比字段显示
        formatData() {
            //整体数据
            var allData = this.shopAnalysisTableDataAll;
            for (var i = 0; i < allData.length; i++) {
                var sumchangenum, rolenum = 0;
                for (var j in allData[i]) {
                    if (j === 'rolenum') {
                        rolenum = allData[i][j];
                    }
                    if (j === 'sumchangenum') {
                        sumchangenum = allData[i][j];
                        allData[i][j] = -allData[i][j];
                    }
                }
                for (var j in allData[i]) {
                    if (j === 'allRolenum') {
                        allData[i][j] = toPercent(rolenum / allData[i][j]);
                    }
                    if (j === 'allSumchangenum') {
                        allData[i][j] = toPercent(sumchangenum / allData[i][j]);
                    }
                }
            }
            this.shopAnalysisTableDataAll = allData;
        },
        /*设置初始值*/
        _init() {
            window.addEventListener('resize', function() {
                /*修改表格的宽度*/
                this.realTimeServiceTableWidth = document.body.clientWidth - 240 + "px";
                this.realTimeServiceContent = document.body.clientWidth - 220 + "px";
            }.bind(this))
        },
        // 表格排序
        sortChange(column) {
            if (column.prop === null) {
                return;
            } else if (column.order === 'descending') {
                this.shopAnalysisTableDataAll.sort(compareObject(column.prop, false));
            } else { //正序
                this.shopAnalysisTableDataAll.sort(compareObject(column.prop, true));
            }
            this.tableControl();
        }
    },
    mounted() {
        this._init()
            // 初始化URL
        this.SRC = this.$store.getters.src;
        this.DP.p = localStorage.getItem('ZULONGBIPROJECTID');
        this.DP.f = localStorage.getItem('ZULONGBIPAGEID');
        //this.getData()
        this.date = [new Date(new Date().getTime() - 8 * 86400000), new Date(new Date().getTime() - 86400000)];
        this.form.dstart = formatGMTDate(new Date(new Date().getTime() - 8 * 86400000), true);
        this.form.dend = formatGMTDate(new Date(new Date().getTime() - 86400000), true);
        this.getData();
        this.search({ dstart: this.form.dstart, dend: this.form.dend, server: this.form.server, param: this.form.param, reason: this.form.reason, rolelevel: this.form.rolelevel })
    }
}